package com.madarasan.providers;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import com.madarasan.SkiDroid.GlobalMenuActivity;
import com.madarasan.SkiDroid.R;
import com.madarasan.resorts.HelloItemizedOverlay;

public class ShowProviderInfo extends GlobalMenuActivity implements
		OnTabChangeListener {

	private static final String LIST_TAB_TAG = "List";
	private static final String MAP_TAB_TAG = "Map";

	private TabHost tabHost;

	private MapView mapView;
	// private MapView mapView2;

	String name;
	String address;
	String telephone;
	Integer latitude;
	Integer longitude;
	String boots;
	String skies;
	String sticks;

	// final MyLocationOverlay myLocation = new MyLocationOverlay(this,
	// mapView);

	@Override
	public void onCreate(Bundle savedInstanceState) {

		Log.i("ShowProviderInfo", "Arrived at start");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.resortproviderlayout);
		// setContentView(R.layout.nn_providerinfo);

		
		displayNotification(getApplicationContext().getResources().getString(R.string.application_finishdownload));
		
		Bundle informations = getIntent().getExtras();

		name = informations.getString("name");
		address = informations.getString("address");
		telephone = informations.getString("telephone");
		latitude = Integer.parseInt(informations.getString("latitude"));
		longitude = Integer.parseInt(informations.getString("longitude"));
		boots = informations.getString("boots");
		skies = informations.getString("skies");
		sticks = informations.getString("sticks");

		TextView nameTV = (TextView) findViewById(R.id.provider_name);
		nameTV.setText(name);

		TextView addressTV = (TextView) findViewById(R.id.provideraddress);
		addressTV.setText(address);

		TextView telephoneTV = (TextView) findViewById(R.id.providertelephone);
		telephoneTV.setText(" Telefon: " + telephone);

		TextView bootsTV = (TextView) findViewById(R.id.providerboots);
		bootsTV.setText(boots + " RON");

		TextView skiesTV = (TextView) findViewById(R.id.providerskies);
		skiesTV.setText(skies + " RON");

		TextView sticksTV = (TextView) findViewById(R.id.providersticks);
		sticksTV.setText(sticks + " RON");

		tabHost = (TabHost) findViewById(android.R.id.tabhost);

		// setup must be called if you are not inflating the tabhost from XML
		tabHost.setup();
		tabHost.setOnTabChangedListener(this);

		// setup map view
		mapView = (MapView) findViewById(R.id.provider_mapview);
		mapView.setBuiltInZoomControls(true);
		mapView.postInvalidate();

		tabHost.addTab(tabHost.newTabSpec(LIST_TAB_TAG)
				.setIndicator(getApplicationContext().getResources().getString(R.string.provider_info_tabname)).setContent(R.id.providertable));

		tabHost.addTab(tabHost.newTabSpec(MAP_TAB_TAG).setIndicator(getApplicationContext().getResources().getString(R.string.provider_map_tabname))
				.setContent(new TabContentFactory() {
					public View createTabContent(String arg0) {
						return mapView;
					}
				}));

		// HACK to get the list view to show up first,
		// otherwise the mapview would be bleeding through and visible
		tabHost.setCurrentTab(1);
		tabHost.setCurrentTab(0);

	}

	/**
	 * Instructs the map view to navigate to the point and zoom level specified.
	 * 
	 * @param geoPoint
	 * @param zoomLevel
	 */
	private void setMapZoomPoint(GeoPoint geoPoint, int zoomLevel) {
		mapView.getController().setCenter(geoPoint);
		mapView.getController().setZoom(zoomLevel);
		mapView.postInvalidate();
	}

	/**
	 * From MapActivity, we ignore it for this demo
	 */
	protected boolean isRouteDisplayed() {
		return false;
	}

	/**
	 * Implement logic here when a tab is selected
	 */
	@SuppressWarnings("unchecked")
	public void onTabChanged(String tabName) {
		if (tabName.equals(MAP_TAB_TAG)) {

			GeoPoint geoPoint = new GeoPoint(latitude, longitude);

			List mapOverlays = mapView.getOverlays();
			Drawable drawable = this.getResources().getDrawable(
					R.drawable.providers_map_item);
			HelloItemizedOverlay itemizedOverlay = new HelloItemizedOverlay(
					drawable);
			OverlayItem overlayitem = new OverlayItem(geoPoint, "Synyx",
					"This is the Synyx office!");
			itemizedOverlay.addOverlay(overlayitem);

			mapOverlays.add(itemizedOverlay);

			final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(
					this, mapView);
			myLocationOverlay.enableMyLocation();
			myLocationOverlay.enableCompass();
			myLocationOverlay.runOnFirstFix(new Runnable() {
				public void run() {
					// move to location
					mapView.getController().animateTo(
							myLocationOverlay.getMyLocation());
					mapView.getController().setZoom(17);
				}
			});
			mapView.getOverlays().add(myLocationOverlay);

			if (geoPoint != null) {
				// have map view moved to this point
				setMapZoomPoint(geoPoint, 20);
			}
		} else if (tabName.equals(LIST_TAB_TAG)) {
			// do something on the list
		}
	}

}